#include "stdafx.h"
#include "UIService.h"
#include "OgreSharedPtr.h"


using namespace Ogre;
using namespace Hikari;

class UIService::UITask : public Task, public IEventHandler
{
public:
	virtual void OnEvent(Event* evt)
	{
		BEGIN_EVENT_HANDLER(evt);

		ON_EVENT(FrameUpdatedEvent)
		{
			g_UIService->UpdateSprite();
		}

		ON_EVENT(InputMouseEvent)
		{
			g_UIService->MouseUpdated(evt);
		}

		END_EVENT_HANDLER;
	}

	virtual bool IsEnabled(){ return true;}
};

UIService::UIService()
{
	m_HikariManager = NULL;
	m_UITask = new UITask();
	MakeSysTask(m_UITask);
	m_UITask->Start();

	m_Sprite = NULL;
}

UIService::~UIService()
{
	m_UITask->Stop();
	SAFE_DELETE(m_UITask);
	SAFE_DELETE(m_HikariManager);
}

void UIService::OnReset()
{
	SAFE_DELETE(m_HikariManager);
	m_HikariManager = new HikariManager("..\\data");

	g_InputService->GetInputChannel()->AddHandler(m_UITask);
}

void UIService::MouseUpdated(InputMouseEvent* evt)
{
	m_HikariManager->injectMouseMove(evt->AX.abs, evt->AY.abs);
	m_HikariManager->injectMouseWheel(evt->AZ.rel);

	if (evt->IsPressed(OIS::MB_Left))
		m_HikariManager->injectMouseDown(OIS::MB_Left);
	else if (evt->IsReleased(OIS::MB_Left))
		m_HikariManager->injectMouseUp(OIS::MB_Left);

	if (evt->IsPressed(OIS::MB_Right))
		m_HikariManager->injectMouseDown(OIS::MB_Right);
	else if (evt->IsReleased(OIS::MB_Right))
		m_HikariManager->injectMouseUp(OIS::MB_Right);

	if (evt->IsPressed(OIS::MB_Middle))
		m_HikariManager->injectMouseDown(OIS::MB_Middle);
	else if (evt->IsReleased(OIS::MB_Middle))
		m_HikariManager->injectMouseUp(OIS::MB_Middle);
}

void UIService::UpdateSprite()
{
	m_HikariManager->update();
}

void UIService::Load()
{
	m_Sprite = new UISprite(m_HikariManager, "control", "controls.swf", 350, 400);
}

void UIService::ChangeVisible()
{
	m_Sprite->ChangeVisible();
}

UISprite::UISprite( Hikari::HikariManager* hikarimgr, const char* name, const char* path, 
					uint width, uint height, Hikari::Position p )
{
	Viewport* vp = g_RenderService->GetViewport("defViewport");

	m_Sprite = hikarimgr->createFlashOverlay(name, vp, width, height, p);
	m_Sprite->load(path);
	m_Sprite->setTransparent(true,true);
//	m_Sprite->setDraggable(false);
	m_Sprite->bind("opacityChange", FlashDelegate(this, &UISprite::onOpacityChange));
	m_Sprite->bind("colorChange", FlashDelegate(this, &UISprite::onColorChange));
	
	m_IsShow = true;
}

UISprite::~UISprite()
{
	
}

void UISprite::Update( float dt )
{

}

Hikari::FlashValue UISprite::onOpacityChange(Hikari::FlashControl* caller, const Hikari::Arguments& args)
{
	Real opacityPercent = args.at(0).getNumber();

	caller->setOpacity(opacityPercent / 100);

	return FLASH_VOID;
}

Hikari::FlashValue UISprite::onColorChange(Hikari::FlashControl* caller, const Hikari::Arguments& args)
{
	ColourValue color = args.at(0).getNumberAsColor();

	g_RenderService->GetViewport("defViewport")->setBackgroundColour(color);

	return FLASH_VOID;
}

void UISprite::ChangeVisible()
{
	if (m_IsShow)
	{
		m_Sprite->hide();
		m_IsShow = false;
	}
	else
	{
		m_Sprite->show();
		m_IsShow = true;
	}
}